From 0f73342f4650313ee9f1f284d89e1331def9d018 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sun, 22 Jan 2023 09:17:35 -0700 Subject: [PATCH] eliminate some time_t uses. (#994) * eliminate some time_t uses. * whittle down time_t * move some gpi time handling to Qt. * drop include comment on defs.h --- defs.h | 1 - garmin_gpi.cc | 20 +++++++------------- gpx.cc | 2 +- gtrnctr.cc | 6 +++--- html.cc | 13 +++---------- kml.cc | 2 +- smplrout.cc | 9 ++++++--- text.cc | 13 ++----------- 8 files changed, 23 insertions(+), 43 deletions(-) diff --git a/defs.h b/defs.h index 6ec2cbc7c..25ef7fe91 100644 --- a/defs.h +++ b/defs.h @@ -184,7 +184,6 @@ struct global_options { extern global_options global_opts; extern const char gpsbabel_version[]; -extern time_t gpsbabel_now; /* gpsbabel startup-time; initialized in main.c with time() */ extern time_t gpsbabel_time; /* gpsbabel startup-time; initialized in main.c with current_time(), ! ZERO within testo ! */ enum fix_type { diff --git a/garmin_gpi.cc b/garmin_gpi.cc index 095cf6781..9a14f2332 100644 --- a/garmin_gpi.cc +++ b/garmin_gpi.cc @@ -24,6 +24,7 @@ #include "garmin_gpi.h" #include // for QByteArray, operator== +#include // for QDateTime #include // for QList #include // for QString, operator+, operator< #include // for QThread @@ -35,10 +36,10 @@ #include // for uint32_t, int32_t #include // for SEEK_CUR, SEEK_SET #include // for strlen, strncmp -#include // for time, gmtime, time_t, tm +#include // for time, time_t #include // for unique_ptr -#include "defs.h" // for Waypoint, fatal, le_write32, le_write16, wp_flags, warning, bounds, KPH_TO_MPS, MPH_TO_MPS, WAYPT_HAS, gpsbabel_testmode, parse_speed, WAYPT_SET, MILES_TO_METERS, MPS_TO_KPH, MPS_TO_MPH, mkgmtime, mkshort, mkshort_del_handle, mkshort_new_... +#include "defs.h" #include "formspec.h" // for FormatSpecificDataList #include "garmin_fs.h" // for garmin_fs_t, garmin_fs_alloc #include "gbfile.h" // for gbfputint32, gbfgetint32, gbfgetint16, gbfputint16, gbfgetc, gbfputc, gbfread, gbftell, gbfwrite, gbfseek, gbfclose, gbfopen_le, gbfgetuint16, gbsize_t, gbfile @@ -174,12 +175,9 @@ GarminGPIFormat::read_header() PP; rdata->crdate = gbfgetint32(fin); if (GPI_DBG) { - char stime[32]; - struct tm tm = *localtime(&rdata->crdate); - tm.tm_year += 20; /* !!! */ - tm.tm_mday -= 1; /* !!! */ - strftime(stime, sizeof(stime), "%Y/%m/%d %H:%M:%S", &tm); - warning("crdate = %lu (%s)\n", rdata->crdate, stime); + time_t crdate = GPS_Math_Gtime_To_Utime(rdata->crdate); + warning("crdate = %lu (%s)\n", rdata->crdate, + CSTR(QDateTime::fromSecsSinceEpoch(crdate, Qt::UTC).toString(Qt::ISODate))); } (void) gbfgetint16(fin); /* 0 */ @@ -1007,11 +1005,7 @@ GarminGPIFormat::write_header() const time_t time = gpi_timestamp; if (time != 0) { - struct tm tm; - tm = *gmtime(&time); - tm.tm_year -= 20; - time = mkgmtime(&tm); - time += SECONDS_PER_DAY; + time = GPS_Math_Utime_To_Gtime(gpi_timestamp); } gbfputint32(0, fout); diff --git a/gpx.cc b/gpx.cc index e0824f56c..246e709af 100644 --- a/gpx.cc +++ b/gpx.cc @@ -565,7 +565,7 @@ GpxFormat::gpx_end(QStringView /*unused*/) */ case tt_cache_log_type: if ((cdatastr.compare(u"Found it") == 0) && - (0 == wpt_tmp->gc_data->last_found.toTime_t())) { + (!wpt_tmp->gc_data->last_found.isValid())) { wpt_tmp->AllocGCData()->last_found = gc_log_date; } gc_log_date = QDateTime(); diff --git a/gtrnctr.cc b/gtrnctr.cc index f127ff829..f53cccef6 100644 --- a/gtrnctr.cc +++ b/gtrnctr.cc @@ -216,13 +216,13 @@ GtrnctrFormat::gtc_fake_hdr(const computed_trkdata& tdata) /* note that the elements must appear in the order required by the schema. */ /* also note some of the elements are required. */ - long secs = 0; + long long secs = 0; if (gtc_least_time.isValid() && gtc_most_time.isValid()) { - secs = gtc_most_time.toTime_t() - gtc_least_time.toTime_t(); + secs = gtc_least_time.secsTo(gtc_most_time); } /* write these in either case, course or activity format */ - gtc_write_xml(0, "%ld\n", secs); + gtc_write_xml(0, "%lld\n", secs); gtc_write_xml(0, "%.2f\n", tdata.distance_meters); if (gtc_course_flag) { /* course format */ gtc_write_xml(1, "\n"); diff --git a/html.cc b/html.cc index 5ffe15b36..54ca38428 100644 --- a/html.cc +++ b/html.cc @@ -28,7 +28,6 @@ #include // for CaseInsensitive #include // for int32_t -#include // for localtime, time_t #include "defs.h" #include "formspec.h" // for FormatSpecificDataList, kFsGpx @@ -151,7 +150,6 @@ HtmlFormat::html_disp(const Waypoint* wpt) const XmlTag* root = fs_gpx->tag; XmlTag* curlog = root->xml_findfirst(u"groundspeak:log"); while (curlog) { - time_t logtime = 0; *file_out << "

\n"; XmlTag* logpart = curlog->xml_findfirst(u"groundspeak:type"); @@ -168,14 +166,9 @@ HtmlFormat::html_disp(const Waypoint* wpt) const logpart = curlog->xml_findfirst(u"groundspeak:date"); if (logpart) { - logtime = xml_parse_time(logpart->cdata).toTime_t(); - struct tm* logtm = localtime(&logtime); - if (logtm) { - *file_out << QStringLiteral("%1-%2-%3
\n") - .arg(logtm->tm_year+1900, 4, 10, QChar('0')) - .arg(logtm->tm_mon+1, 2, 10, QChar('0')) - .arg(logtm->tm_mday, 2, 10, QChar('0')); - } + gpsbabel::DateTime logtime = xml_parse_time(logpart->cdata).toLocalTime(); + *file_out << "" + << logtime.toString(u"yyyy-MM-dd") << "
\n"; } logpart = curlog->xml_findfirst(u"groundspeak:log_wpt"); diff --git a/kml.cc b/kml.cc index 1d9fa281d..818e92ea1 100644 --- a/kml.cc +++ b/kml.cc @@ -1953,7 +1953,7 @@ void KmlFormat::wr_position(Waypoint* wpt) last_valid_fix = wpt->GetCreationTime(); } - wpt->icon_descr = kml_get_posn_icon(wpt->GetCreationTime().toTime_t() - last_valid_fix.toTime_t()); + wpt->icon_descr = kml_get_posn_icon(last_valid_fix.secsTo(wpt->GetCreationTime())); /* In order to avoid clutter while we're sitting still, don't add diff --git a/smplrout.cc b/smplrout.cc index 405ab3da6..88d6db0e3 100644 --- a/smplrout.cc +++ b/smplrout.cc @@ -133,9 +133,12 @@ void SimplifyRouteFilter::compute_xte(struct xte* xte_rec) fatal(MYNAME ": relative needs hdop information.\n"); } // if timestamps exist, distance to interpolated point - if (wpt1->GetCreationTime() != wpt2->GetCreationTime()) { - double frac = (double)(wpt3->GetCreationTime().toTime_t() - wpt1->GetCreationTime().toTime_t()) / - (wpt2->GetCreationTime().toTime_t() - wpt1->GetCreationTime().toTime_t()); + if (wpt1->GetCreationTime().isValid() && + wpt2->GetCreationTime().isValid() && + wpt3->GetCreationTime().isValid() && + (wpt1->GetCreationTime() != wpt2->GetCreationTime())) { + double frac = static_cast(wpt1->GetCreationTime().msecsTo(wpt3->GetCreationTime())) / + static_cast(wpt1->GetCreationTime().msecsTo(wpt2->GetCreationTime())); linepart(wpt1->latitude, wpt1->longitude, wpt2->latitude, wpt2->longitude, frac, &reslat, &reslon); diff --git a/text.cc b/text.cc index a0daa10ef..b582abf5e 100644 --- a/text.cc +++ b/text.cc @@ -21,14 +21,12 @@ #include "text.h" -#include // for QChar #include // for QIODevice, QIODevice::WriteOnly #include // for QString, operator!= #include // for QTextStream #include // for CaseInsensitive #include // for int32_t -#include // for localtime, time_t #include "defs.h" #include "formspec.h" // for FormatSpecificDataList, kFsGpx @@ -134,7 +132,6 @@ TextFormat::text_disp(const Waypoint* wpt) XmlTag* root = fs_gpx->tag; XmlTag* curlog = root->xml_findfirst(u"groundspeak:log"); while (curlog) { - time_t logtime = 0; *file_out << "\n"; XmlTag* logpart = curlog->xml_findfirst(u"groundspeak:type"); @@ -149,14 +146,8 @@ TextFormat::text_disp(const Waypoint* wpt) logpart = curlog->xml_findfirst(u"groundspeak:date"); if (logpart) { - logtime = xml_parse_time(logpart->cdata).toTime_t(); - struct tm* logtm = localtime(&logtime); - if (logtm) { - *file_out << QStringLiteral("%1-%2-%3\n") - .arg(logtm->tm_year+1900, 4, 10, QChar('0')) - .arg(logtm->tm_mon+1, 2, 10, QChar('0')) - .arg(logtm->tm_mday, 2, 10, QChar('0')); - } + gpsbabel::DateTime logtime = xml_parse_time(logpart->cdata).toLocalTime(); + *file_out << logtime.toString(u"yyyy-MM-dd") << "\n"; } logpart = curlog->xml_findfirst(u"groundspeak:log_wpt"); -- 2.30.2